package cn.cloudplug.friendsrecommend.fragment;


import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;

import cn.cloudplug.friendsrecommend.AgreementActivity;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.util.Constant;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegFragment extends Fragment implements OnClickListener {
	private EditText et_username;
	private EditText et_yanzheng;
	private EditText et_pwd;
	private EditText et_confirepwd;
	private TextView tv_protocol;
	private CheckBox cb_protocol;

	UMSocialService mController = UMServiceFactory
			.getUMSocialService("com.umeng.share");

	public RegFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.activity_reg, container, false);
		view.findViewById(R.id.tv_getyanzhengma).setOnClickListener(this);
		view.findViewById(R.id.btn_reg).setOnClickListener(this);
		cb_protocol=(CheckBox) view.findViewById(R.id.cb_protocol);
		cb_protocol.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), isChecked?"同意协议":"拒绝协议", 0).show();
			}
		});
		tv_protocol=(TextView) view.findViewById(R.id.tv_protocol);
		tv_protocol.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		tv_protocol.setOnClickListener(this);
		et_username = (EditText) view.findViewById(R.id.et_username);
		et_yanzheng = (EditText) view.findViewById(R.id.et_yanzhengma);
		et_pwd = (EditText) view.findViewById(R.id.et_password);
		et_confirepwd = (EditText) view.findViewById(R.id.et_password_confirm);
		return view;
	}

	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_reg:
			doRegister();
			break;
		case R.id.tv_getyanzhengma:
			Toast.makeText(getActivity(), "验证码已经发送", 0).show();
			break;
		case R.id.tv_protocol:
			getActivity().startActivity(new Intent(getActivity(),AgreementActivity.class));
			break;
		}
	}

	// 登录按钮点击事件
	private void doRegister() {
		String username = et_username.getText().toString().trim();
		String yanzheng = et_yanzheng.getText().toString().trim();
		String pwd = et_pwd.getText().toString().trim();
		String confirepwd = et_confirepwd.getText().toString().trim();
		if (TextUtils.isEmpty(username)) {
			et_username.setError("用户名不能为空");
			return;
		}
		if (TextUtils.isEmpty(yanzheng)) {
			et_yanzheng.setError("验证码不能为空");
			return;
		}
		if (TextUtils.isEmpty(pwd)) {
			et_pwd.setError("密码不能为空");
			return;
		}
		if (TextUtils.isEmpty(confirepwd)) {
			et_confirepwd.setError("确认密码不能为空");
			return;
		}
		if ("zhangsan".equals(username) && "123".equals(pwd)) {
			Intent intent = new Intent();
			intent.setAction(Constant.LOGIN_SUCCESS);
			getActivity().sendBroadcast(intent);
			getActivity().finish();
			getActivity().overridePendingTransition(R.anim.activity_out,R.anim.b_activity_out);
		}
	}

	
}
